import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

//push
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

//replace
const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(to) {
  return VueRouterReplace.call(this, to).catch((err) => err);
};

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login"),
    meta: {
      title: "后台登录",
    },
  },
  {
    path: "/portal",
    name: "portal",
    redirect: "/user/commonUser", // 访问portal，默认跳转到 /user/commonUser
    component: () => import("@/views/portal"),
    children: [
      /* 用户管理 */
      {
        path: "/user/commonUser",
        name: "commonUser",
        component: () => import("@/views/sys/user/common-user"),
        meta: {
          title: "会员管理",
        },
      },
      {
        path: "/user/adminUser",
        name: "adminUser",
        component: () => import("@/views/sys/user/admin-user"),
        meta: {
          title: "后台用户管理",
        },
      },
      /* 分类管理 */
      {
        path: "/category/firstCategory",
        name: "firstCategory",
        component: () => import("@/views/sys/category/first-category"),
        meta: {
          title: "一级分类",
        },
      },
      {
        path: "/category/secondCategory",
        name: "secondCategory",
        component: () => import("@/views/sys/category/second-category"),
        meta: {
          title: "二级分类",
        },
      },
      {
        path: "/category/thirdCategory",
        name: "thirdCategory",
        component: () => import("@/views/sys/category/third-category"),
        meta: {
          title: "三级分类",
        },
      },
      /* 商品管理 */
      {
        path: "/commodity/goods",
        name: "commodity",
        component: () => import("@/views/sys/commodity/goods"),
        meta: {
          title: "商品管理",
        },
      },
      {
        path: "/commodity/goods-type",
        name: "type",
        component: () => import("@/views/sys/commodity/goods-type"),
        meta: {
          title: "型号管理",
        },
      },
      /* 数据分析 */
      {
        path: "/analysis/userAnalysis",
        name: "userAnalysis",
        component: () => import("@/views/sys/analysis/user-analysis"),
        meta: {
          title: "用户分析",
        },
      },
      {
        path: "/analysis/commodityAnalysis",
        name: "commodityAnalysis",
        component: () => import("@/views/sys/analysis/commodity-analysis"),
        meta: {
          title: "商品分析",
        },
      },
      {
        path: "/analysis/commentAnalysis",
        name: "commentAnalysis",
        component: () => import("@/views/sys/analysis/comment-analysis"),
        meta: {
          title: "评论统计",
        },
      },
      /* 系统管理 */
      {
        path: "/system/authority",
        name: "authority",
        component: () => import("@/views/sys/system/authority"),
        meta: {
          title: "权限管理",
        },
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 导航守卫
router.beforeEach((to, form, next) => {
  // 设置页面的标题
  const title = to.meta.title;
  if (title) {
    document.title = title;
  }
  // 放行
  next();
});

export default router;
